package com.ruoyi.stuinfo.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.stuinfo.domain.AbnormalStudentDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stuinfo.mapper.AttendancerecordMapper;
import com.ruoyi.stuinfo.domain.Attendancerecord;
import com.ruoyi.stuinfo.service.IAttendancerecordService;

/**
 * 考勤记录Service业务层处理
 *
 * @author zc
 * @date 2025-05-14
 */
@Service
public class AttendancerecordServiceImpl implements IAttendancerecordService
{
    @Autowired
    private AttendancerecordMapper attendancerecordMapper;

    /**
     * 查询考勤记录
     *
     * @param recordId 考勤记录主键
     * @return 考勤记录
     */
    @Override
    public Attendancerecord selectAttendancerecordByRecordId(Long recordId)
    {
        return attendancerecordMapper.selectAttendancerecordByRecordId(recordId);
    }

    /**
     * 查询考勤记录列表
     *
     * @param attendancerecord 考勤记录
     * @return 考勤记录
     */
    @Override
    public List<Attendancerecord> selectAttendancerecordList(Attendancerecord attendancerecord)
    {
        return attendancerecordMapper.selectAttendancerecordList(attendancerecord);
    }

    /**
     * 新增考勤记录
     *
     * @param attendancerecord 考勤记录
     * @return 结果
     */
    @Override
    public int insertAttendancerecord(Attendancerecord attendancerecord)
    {
        return attendancerecordMapper.insertAttendancerecord(attendancerecord);
    }

    /**
     * 修改考勤记录
     *
     * @param attendancerecord 考勤记录
     * @return 结果
     */
    @Override
    public int updateAttendancerecord(Attendancerecord attendancerecord)
    {
        return attendancerecordMapper.updateAttendancerecord(attendancerecord);
    }

    /**
     * 批量删除考勤记录
     *
     * @param recordIds 需要删除的考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteAttendancerecordByRecordIds(Long[] recordIds)
    {
        return attendancerecordMapper.deleteAttendancerecordByRecordIds(recordIds);
    }

    /**
     * 删除考勤记录信息
     *
     * @param recordId 考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteAttendancerecordByRecordId(Long recordId)
    {
        return attendancerecordMapper.deleteAttendancerecordByRecordId(recordId);
    }

    @Override
    public List<Attendancerecord> selectAttendancerecordList_handteacher(String teacherId) {
        return attendancerecordMapper.selectAttendancerecordList_handteacher(teacherId);
    }

    @Override
    public List<Attendancerecord> selectAttendancerecordList_Xiteacher(String teacherId) {
        return attendancerecordMapper.selectAttendancerecordList_Xiteacher(teacherId);
    }



    @Override
    public Map<String, Object> getDormitoryStats() {
        Map<String, Long> counts = attendancerecordMapper.countAttendanceStatus();

        Map<String, Object> result = new HashMap<>();
        result.put("normal", counts.getOrDefault("normal", 0L));
        result.put("late", counts.getOrDefault("late", 0L));
        result.put("absent", counts.getOrDefault("absent", 0L));

        return result;
    }
    @Override
    public Map<String, Object> getAbnormalStudents(int pageNum, int pageSize, String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("pageSize", pageSize);

        // 只添加有效的type参数
        if (type != null && !type.trim().isEmpty() && ("晚归".equals(type) || "不归".equals(type))) {
            params.put("type", type);
        }

        List<AbnormalStudentDTO> students = attendancerecordMapper.selectAbnormalStudents(params);
        int total = attendancerecordMapper.countAbnormalStudents(params);

        Map<String, Object> result = new HashMap<>();
        result.put("list", students != null ? students : Collections.emptyList());
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);

        System.out.println("查询到的学生列表: " + students); // 打印查询结果，用于调试
        System.out.println("总记录数: " + total);

        return result;
    }
}
